package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	//if测试
	readFile()

	//switch测试
	fmt.Println(grade(79))
	fmt.Println(
		grade(90),
		//触发panic, 这个函数的执行会被终止, 90的计算结果也不会被打印
		grade(999),
	)

	//map测试
	testGetAdmin()
}

func readFile() {
	/*
		//常规
		const filename = "go-new/go-01-basic/test.txt"
		contents, err := ioutil.ReadFile(filename)

		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Printf("%s \n", contents)
		}
	*/

	const filename = "go-new/go-01-basic/test.txt"
	if contents, err := ioutil.ReadFile(filename); err == nil {
		fmt.Println(string(contents))
	} else {
		fmt.Println("print file err: ", err)
	}
}

func grade(score int) string {
	g := ""
	//switch 后面可以有表达式, 也可以没有表达式
	switch {
	case score < 60:
		g = "C"
	case score < 80:
		g = "B"
	case score <= 100:
		g = "A"
	default:
		panic(fmt.Sprintf("Wrong score: %d", score))
	}
	return g
}
func variableDefaultValue() {
	var digit int
	var str string
	fmt.Printf("%d %q \n", digit, str)
}

func testGetAdmin() {
	tsMap := map[string]bool{
		"1": true,
		"2": true,
		"3": false,
		"4": false,
	}
	delete(tsMap, "3")
	fmt.Println(GetAdmin(false, tsMap))
}

func GetAdmin(all bool, tsMap map[string]bool) []string {
	admin := []string{}
	for k, ok := range tsMap {
		b := all || !ok
		if b {
			admin = append(admin, k)
		}
	}
	return admin
}
